import 'package:flutter/material.dart';
// import 'package:date_format/date_format.dart';

class DatePickerDemo extends StatefulWidget {
  DatePickerDemo({Key key}) : super(key: key);

  @override
  _DatePickerDemoState createState() => _DatePickerDemoState();
}

class _DatePickerDemoState extends State<DatePickerDemo> {
  // var now = DateTime.now();
  DateTime _nowDate = DateTime.now();
  
  // void _showDatePicker() {
  //   // 调用日期组件
  //   showDatePicker(
  //     context: context,
  //     initialDate: _nowDate,
  //     firstDate: DateTime(1980),
  //     lastDate: DateTime(2021)
  //   )
  //     .then((value) => print("asd${value}"))
  //     .catchError((err) => print(err));
  // }

  void _showDatePicker() async {
    // 调用日期组件
    DateTime result = await showDatePicker(
      context: context,
      initialDate: _nowDate,
      firstDate: DateTime(1980),
      lastDate: DateTime(2021)
    );
    
    // var Time = TimeOfDay(hour: 10, minute: 30);
    // Time.format(context);
    print(result);
    setState(() {
      this._nowDate = result;
    });
  }
  
  @override
  void initState() {
    super.initState();
    // 日期和时间戳的相互转换
    // print(now); // 2020-09-28 22:25:04.329374
    // print(now.millisecondsSinceEpoch); // 1601303065030
    // print(DateTime.fromMicrosecondsSinceEpoch(1601303065030)); // 2020-09-28 22:25:04.329374
    // print(formatDate(DateTime.now(), [yyyy, '年', mm, '月', dd]));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
       child: Scaffold(
          appBar: AppBar(
            title: Text("DatePicker")
          ),
          body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              InkWell(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text("${_nowDate}"),
                    Icon(Icons.arrow_drop_down)
                  ],
                ),
                onTap: _showDatePicker,
              )
            ],
          )
       ),
    );
  }
}